﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.General;
using PPI.WebConsumer.PPI.Svc;
using EPhase=PPI.Entities.Distribution.EPhase;

namespace PPI.WebConsumer.Distribution
{
    public class WDistributionPolePhase
    {
        public static DataSet SelectAllIdName()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            Service1 svc = new Service1();

            return svc.DistributionPhases_SelectAllIdName(_Reader.ConectionMode);
        }

        public static List<EPhase> Save(List<EPhase> phases, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.EPhase> wPhases = new List<PPI.Svc.EPhase>();
            foreach (EPhase phase in phases)
            {
                PPI.Svc.EPhase ph = new PPI.Svc.EPhase();

                ph.Id = phase.Id;
                ph.Key = phase.Key;
                ph.Name = phase.Name;

                wPhases.Add(ph);
            }

            PPI.Svc.EPhase[] wPhasesResult = svc.DistributionPhase_Save(poleId, wPhases.ToArray(), reader.GetConnectionString());
            phases = new List<EPhase>();
            foreach (PPI.Svc.EPhase phaseResult in wPhasesResult)
            {
                EPhase eResp = new EPhase();

                eResp.ServerId = phaseResult.ServerId;
                eResp.Id = phaseResult.Id;
                eResp.Key = phaseResult.Key;
                eResp.Name = phaseResult.Name;

                phases.Add(eResp);
            }

            return phases;
        }

        public static List<EPhase> Update(List<EPhase> phases, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.EPhase> wPhases = new List<PPI.Svc.EPhase>();
            foreach (EPhase phase in phases)
            {
                PPI.Svc.EPhase ph = new PPI.Svc.EPhase();

                ph.Id = phase.Id;
                ph.Key = phase.Key;
                ph.Name = phase.Name;

                wPhases.Add(ph);
            }

            PPI.Svc.EPhase[] wPhasesResult = svc.DistributionPhase_Update(poleId, wPhases.ToArray(), reader.GetConnectionString());
            phases = new List<EPhase>();
            foreach (PPI.Svc.EPhase phaseResult in wPhasesResult)
            {
                EPhase eResp = new EPhase();

                eResp.ServerId = phaseResult.ServerId;
                eResp.Id = phaseResult.Id;
                eResp.Key = phaseResult.Key;
                eResp.Name = phaseResult.Name;

                phases.Add(eResp);
            }

            return phases;
        }
    }
}
